/**
 * This file is part of the FlexiTimer project.
 * 
 * Author: Florian Arndt <f.aarndt@web.de>
 *  	   Clemens Teichmann <>
 *
 * http://code.google.com/p/flexitimer/ 
 */

package flexitimer_client.gui;

import java.awt.*;
import java.awt.TrayIcon.MessageType;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

import flexitimer_client.calc.*;

public class frame extends JFrame
{
	private static 	frame 			instance 				= null;
	public 			updateAction 	update 					= new updateAction();
					JButton			button1					= new JButton(new startTimeAction());
					JButton			button2 				= new JButton(new endTimeAction());
					JButton			button3 				= new JButton(update);
					JButton 		button4 				= new JButton(new startTimeMinAction());
					JButton			button5					= new JButton(new endNowAction());
					JLabel 			startschrift 			= new JLabel("Uhrzeit Einstempeln: ");
					JLabel 			endeschrift 			= new JLabel("Uhrzeit Ausstempeln: ");
					JTextField 		start 					= new JTextField("08:00");
					JTextField 		ende 					= new JTextField("15:20");
					TextArea 		area 					= new TextArea("", 15, 40);
					JPanel 			center 					= new JPanel();
					JPanel 			center1 				= new JPanel();
					JPanel 			center2 				= new JPanel();
					JPanel			buttonPanel				= new JPanel();
					JLabel 			head 					= new JLabel();
					JLabel 			progress 				= new JLabel();
					ImageIcon		clockIcon				= new ImageIcon(getClass().getClassLoader().getResource("clock.png"));
					TrayIcon		trayIcon				= new TrayIcon(new ImageIcon(getClass().getClassLoader().getResource("clocksmall.png")).getImage());
					MenuItem 		startCountdownMenuItem 	= new MenuItem("Start Countdown");
					MenuItem 		exitMenuItem 			= new MenuItem("Beenden");
					MenuItem		optionMenuItem			= new MenuItem("Einstellungen");

	private frame() {
		super("FlexiTimer - Gleitzeit Berechnung");
		initGUI();
		initMenu();
		initAction();
		initSystemTrayIcon();
	}	
	
		public static frame getInstance() {
		if (instance == null){ instance = new frame(); }
		return instance;
	}
		@SuppressWarnings("deprecation")
	private void initGUI()
	{
		setLayout(new BorderLayout());
		setSize(600, 500);
		setResizable(false);
		this.setIconImage(clockIcon.getImage());

		area.setFocusable(false);
		area.setEditable(false);
		area.setPreferredSize(new Dimension(30, 40));

		start.setNextFocusableComponent(ende);
		ende.setNextFocusableComponent(button1);
		button4.setNextFocusableComponent(button2);
		
		center.setLayout(new FlowLayout());
		center1.setLayout(new GridLayout(3, 3));
		center1.add(startschrift);
		center1.add(start);
		center1.add(button1);
		center1.add(new JLabel(""));
		center1.add(new JLabel(""));
		center1.add(button4);
		center1.add(endeschrift);
		center1.add(ende);
		center1.add(button2);
				
		button3.addActionListener(autoUpdateAction.getInstance());

		buttonPanel.setLayout(new GridLayout(2, 1));
		buttonPanel.add(button3);
		buttonPanel.add(button5);
		
		center2.add(getContentPane().add(new JScrollPane().add(area)));
		center2.add(buttonPanel);

		head.setText("Copyright 2010 - Florian Arndt");
		head.setFont(new Font("ComicSan", Font.BOLD, 10));

		center.add(center1);
		center.add(center2);

		//add(head, BorderLayout.NORTH);
		add(center, BorderLayout.CENTER);
		progress.setText("FlexiTimer gestartet...");
		progress.setBorder(BorderFactory.createLoweredBevelBorder());
		add(progress,BorderLayout.SOUTH);
	}
	
	private void initAction()
	  {
		this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
			this.addWindowListener(new WindowListener(){
				public void windowActivated(WindowEvent arg0) {}
				public void windowClosed(WindowEvent arg0) {}
				public void windowClosing(WindowEvent arg0) {frame.getInstance().dispose();}
				public void windowDeactivated(WindowEvent arg0) {}
				public void windowDeiconified(WindowEvent arg0) {}
				public void windowIconified(WindowEvent arg0) {}
				public void windowOpened(WindowEvent arg0) {}
				});
	  }

	private void initMenu()
	{
		JMenu fileMenu = new JMenu("Datei");
		fileMenu.add(deleteAction.getInstance());
		fileMenu.add(optionAction.getInstance());
		fileMenu.add(new JSeparator());
		fileMenu.add(ExitAction.getInstance());

		JMenu fileMenu2 = new JMenu("?");
		fileMenu2.add(dlrAction.getInstance());
		fileMenu2.add(new JSeparator());
		fileMenu2.add(infoAction.getInstance());
		
		JMenuBar myMenuBar = new JMenuBar();
		myMenuBar.add(fileMenu);
		myMenuBar.add(fileMenu2);

		this.setJMenuBar(myMenuBar);
	}
	
	private void initSystemTrayIcon() {
		
		if(!SystemTray.isSupported()){
			System.out.println("SystemTray not supported");
			return;
		}
		final PopupMenu trayMenu = new PopupMenu();
		trayIcon.setToolTip("FlexiTimer");
		final SystemTray tray = SystemTray.getSystemTray();
		
		startCountdownMenuItem.addActionListener(autoUpdateAction.getInstance());
		startCountdownMenuItem.addActionListener(update);
		exitMenuItem.addActionListener(ExitAction.getInstance());
		optionMenuItem.addActionListener(optionAction.getInstance());
		
		trayMenu.add(startCountdownMenuItem);
		trayMenu.add(optionMenuItem);
		trayMenu.addSeparator();
		trayMenu.add(exitMenuItem);
		
		trayIcon.addMouseListener(new MouseListener(){
			public void mouseClicked(MouseEvent mEvent) {
				if(mEvent.getClickCount()%2 == 0) {
					if(mEvent.getButton() == 1 && frame.getInstance().isVisible())
							{frame.getInstance().dispose();}
					else 
						if(mEvent.getButton() == 1 && !frame.getInstance().isVisible())
								{frame.getInstance().setVisible(true);}
				}
				else {
					if(frame.getInstance().isVisible()){frame.getInstance().show();}
				}
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseReleased(MouseEvent arg0) {}
		});
		
		trayIcon.setPopupMenu(trayMenu);
		try{
			tray.add(trayIcon);
		} catch (AWTException e){
			System.out.println("TrayIcon could not be added.");
		}
	}

	public void addText(String ausgabe)
	{ area.append(ausgabe + "\n"); }

	public String getStartTime() { 
		if(start.getText().length() == 4) {
			String hour = start.getText().substring(0, 2);
			String min  = start.getText().substring(2, 4);
			return hour+":"+min;
		}
		if(start.getText().charAt(2) == '.') {
			String hour = start.getText().substring(0, 2);
			String min  = start.getText().substring(3, 5);
			return hour+":"+min;
		}
		else { return start.getText(); }
	}

	public String getEndTime() {
		if(ende.getText().length() == 4) {
			String hour = ende.getText().substring(0, 2);
			String min  = ende.getText().substring(2, 4);
			return hour+":"+min;
		}
		if(ende.getText().charAt(2) == '.') {
			String hour = ende.getText().substring(0, 2);
			String min  = ende.getText().substring(3, 5);
			return hour+":"+min;
		}
		else { return ende.getText(); }
	}

	public void deleteText()
	{ area.setText(""); }
	
	public void addProgress(String countdown) {
		progress.setText("FlexiTimer gestartet..." + countdown);
	}
	
	public void setFrameTitle(String newTitle){
		this.setTitle("FlexiTimer - "+newTitle);
	}
	
	public void trayPopupMessage(String caption, String text) {
		trayIcon.displayMessage(caption, text, MessageType.NONE);
	}
	public void setCountdownButton(String buttonTitle) {
		this.button3.setText(buttonTitle);
	}
	public void setCountdownPopupButton(String buttonTitle) {
		this.startCountdownMenuItem.setLabel(buttonTitle);
	}
	
}
